<?php /* УТФ-8:fix */

class SPIcontroller__manage__menu extends SPIcontroller__dummy
{
    public function build()
    {
        $menu = array();

        $controllers = array(
            'content',
            'consts',
            'singlepage',
            //'songnvideo',
            //'logos',
            'logout',
            'profiles',
            //'feedback',
            'catalogs',
            'brands',
        );

        $items_by_toolbars = array();
        $active_toolbar_path = null;

        foreach( $controllers as $topic_id )
        {
            $cBaseObject = SPIc::I($topic_id);

            if( is_object( $cBaseObject ) === false )
            {
                continue;
            }

            if( is_object( $cBaseObject->manage ) === false )
            {
                continue;
            }

            if( method_exists( $cBaseObject->manage, 'gen_menu' ) === false )
            {
                continue;
            }

            $menuitems = $cBaseObject->manage->gen_menu();

            if( is_array( $menuitems ) === false )
            {
                continue;
            }

            foreach( $menuitems as $toolbar_id => $subitems )
            {
                if( is_array( $subitems ) === false )
                {
                    continue;
                }

                $toolbar_id = strtolower( $toolbar_id );
                $toolbar_id = str_replace( '->', '.', $toolbar_id );

                foreach( $subitems as $menuitem )
                {
                    if( is_array( $menuitem['access'] ) === false )
                    {
                        continue;
                    }

                    $is_any_permit = false;

                    foreach( $menuitem['access'] as $access_permit )
                    {
                        if( SPI::I('Iam')->is_permit( $access_permit ) === true )
                        {
                            $is_any_permit = true;
                            break;
                        }
                    }

                    if( $is_any_permit === false )
                    {
                        continue;
                    }

                    // ------------------------------------------------------------------

                    $items_by_toolbars[$toolbar_id][] = $menuitem;

                    if( $menuitem['is_active'] === true && $active_toolbar_path === null )
                    {
                        $active_toolbar_id = $toolbar_id;
                    }
                }
            }
        }

        if( $active_toolbar_id === null )
        {
            $open_toolbar = SPI::I('var')->get('open_toolbar');
            $active_toolbar_id = $open_toolbar ? $open_toolbar : 'main';
        }

        return $this->subbuild_recursive( $items_by_toolbars, $active_toolbar_id );
    }

    private function subbuild_recursive( & $items_by_toolbars, $active_toolbar_id )
    {
        static $recursive_level     = 10;
        if( --$recursive_level == 0 ) trigger_error( 'Too high recursive on menu build', E_USER_ERROR );

        static $menu                    = array();
        static $toolbar_bricks_id       = null;
        static $curlevel_toolbar_id     = '';
        static $nextlevel_toolbar_title = '';

        if( $toolbar_bricks_id === null )
        {
            $toolbar_bricks_id = explode( '.', $active_toolbar_id );
        }

        $curlevel_menu = array( 'left' => array(), 'right' => array() );
        $item_index    = 0;

        $curlevel_toolbar_id .= ( $curlevel_toolbar_id ? '.' : '' ) . array_shift( $toolbar_bricks_id );

        $menu[] = array(
            'title' => $nextlevel_toolbar_title,
            'items' => array(),
        );

        // ------------------------------------------------------------------------------

        if( isset( $items_by_toolbars[$curlevel_toolbar_id] ) && is_array( $items_by_toolbars[$curlevel_toolbar_id] ) )
        foreach( $items_by_toolbars[$curlevel_toolbar_id] as $menuitem )
        {
            $menuitem['type'] = 'item';
            $menuitem['align_prior'] = isset( $menuitem['align_prior'] ) ? min( max( 1, (int) $menuitem['align_prior'] ), 9 ) : 5;

            if( in_array( $menuitem['align'], array( 'left', 'right' ) ) === false )
            {
                $menuitem['align'] = 'left';
            }

            $uni_key = $menuitem['align_prior'] . $menuitem['name'] . (++$item_index);
            $curlevel_menu[ $menuitem['align'] ][$uni_key] = $menuitem;
        }

        // ------------------------------------------------------------------------------

        $exists_toolbar_ids = array();

        foreach( $items_by_toolbars as $toolbar_key => $menuitems )
        {
            if( preg_match( '|^' . preg_quote("{$curlevel_toolbar_id}.",'|') . '([a-z0-9_]+)|ui', $toolbar_key, $matches ) == 0 )
            {
                continue;
            }
            $toolbar_key_nextlevel = $curlevel_toolbar_id.'.'.$matches[1];

            if( in_array( $toolbar_key_nextlevel, $exists_toolbar_ids ) === true )
            {
                continue;
            }
            $exists_toolbar_ids[] = $toolbar_key_nextlevel;

            $menuitem = $this->get_toolbar_info( $active_toolbar_id, $toolbar_key_nextlevel );
            $menuitem['align_prior'] = isset( $menuitem['align_prior'] ) ? min( max( 1, (int) $menuitem['align_prior'] ), 9 ) : 5;

            if( in_array( $menuitem['align'], array( 'left', 'right' ) ) === false )
            {
                $menuitem['align'] = 'left';
            }

            if( $menuitem['is_active'] )
            {
                $nextlevel_toolbar_title = $menuitem['title'];
            }

            $uni_key = $menuitem['align_prior'] . $menuitem['name'] . (++$item_index);
            $curlevel_menu[ $menuitem['align'] ][$uni_key] = $menuitem;
        }

        ksort( $curlevel_menu['left'] );
        krsort( $curlevel_menu['right'] );

        $menu[ count($menu)-1 ]['items'] = $curlevel_menu;

        if( count( $toolbar_bricks_id ) > 0 )
        {
            $this->subbuild_recursive( $items_by_toolbars, $active_toolbar_id );
        }

        return $menu;
    }

    private function get_toolbar_info( $active_toolbar_id, $toolbar_key )
    {
        $short_toolbar_key = $toolbar_key;
        $short_toolbar_key = preg_replace( '!^main\.!u', '', $short_toolbar_key );
        $short_toolbar_key = str_replace( '.', '__', $short_toolbar_key );

        $align          = 'left';
        $align_prior    = 5;

        switch( $toolbar_key )
        {
            case 'main.funs':
            {
                $align_prior = 7;
                break;
            }
            case 'main.system':
            {
                $align_prior = 9;
                break;
            }
        }

        $toolbar = array(
            'type'          => 'toolbar',
            'name'          => SPI::I('lang')->manage->{"toolbar_{$short_toolbar_key}_name"},
            'title'         => SPI::I('lang')->manage->{"toolbar_{$short_toolbar_key}_title"},
            'icon'          => "toolbar__manage__{$short_toolbar_key}.gif",
            'align'         => $align,
            'align_prior'   => $align_prior,
            'link'          => SPIc::I('manage')->pack_link( array( 'open_toolbar' => $toolbar_key ) ),
            'is_active'     => ( $active_toolbar_id == $toolbar_key ) || strpos( $active_toolbar_id, "{$toolbar_key}." ) !== false 
        );

        return $toolbar;
    }
}
